package com.example.administrator.yuejian_2.activity;

import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.interservice.Const;

import java.io.File;

public class MaclassActivity extends AppCompatActivity implements Button.OnClickListener {
    private VideoView videoView;
    private Button button_start;
    private Button button_pause;
    private String path="";
    private String BASEURL= "http://10.0.2.2/YueJian16/";
    private ProgressDialog dialog;
    private TextView textView;
    private TextView title;
    private ImageButton backButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maclass);


        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置屏幕方向为纵向
       // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
        getWindow().setFormat(PixelFormat.TRANSLUCENT);//透明

        title = (TextView) findViewById(R.id.macla_title);
        title.setText(getIntent().getStringExtra("title"));

        backButton = (ImageButton) findViewById(R.id.back_imageButton8);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        videoView = (VideoView) findViewById(R.id.videoView);
        textView = (TextView) findViewById(R.id.intro_textView);
        textView.setText(getIntent().getStringExtra("intro"));

        path=BASEURL+"Public/video/"+getIntent().getStringExtra("vname");//获取视频全路径
        dialog=ProgressDialog.show(MaclassActivity.this, "视频加载中...", "请您稍候");//进度条

        Uri uri = Uri.parse(path);
        videoView.setMediaController(new MediaController(this));//媒体播放控制工具
        videoView.setVideoURI(uri);//设置视频路径
        videoView.setOnPreparedListener(new MyPlayerOnPreparedListener());//播放开始回调
        videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());//播放完成回调
       // videoView.requestFocus();// 让VideoView获取焦点
        // videoView.start();//开始播放

        button_start = (Button) findViewById(R.id.start_button);
        button_start.setOnClickListener(this);

        button_pause = (Button) findViewById(R.id.pause_button);
        button_pause.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_button:
                videoView.start();
                break;

            case R.id.pause_button:
                videoView.pause();
                break;
        }

    }

    class MyPlayerOnPreparedListener implements MediaPlayer.OnPreparedListener {
        @Override
        public void onPrepared(MediaPlayer mp) {
            videoView.setBackgroundColor(Color.argb(0, 0, 255, 0));
            dialog.dismiss();
        }
    }
    //自定义子类，监听播放完成，显示完成
    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText( MaclassActivity.this, "播放完成", Toast.LENGTH_SHORT).show();
            //getSupportActionBar().show();
        }
    }

}
